/**
 * Created by zcb on 15-7-11.
 */

define([
	'../../Shop/module/STH_main.js',
	'iCheck',
	'selectModify',
	'plupload',
	'colorpicker',
	'layer'
], function(main, iCheck, selectModify, plupload, colorpicker, layer) {

	return {
		/**--navs类型start--*/
		//加入text类型模块
		navsAddM: function(con) {
			var _this = this,
				key = con.key,
				order = con.order,

				navStyle = con.prop.navStyle,
				iconStyle = con.prop.iconStyle,
				 
				navLlist = con.NavList,
				listLen = navLlist.length,

				newPanel = main.getPanel("navs"),
				newEdit = main.getEdit("navs");
				
				con.ItemList=navLlist;
				con.NavList=[];
			//属性操作
			$(newPanel).attr("order", order);
			$(newPanel).attr("key", key);
			$(newEdit).attr("key", key);
			$(newEdit).css("display", "none");

			if(navStyle == 'style1') {
				if(iconStyle == 'icon_none') {
					iconStyle = 'icon_top';
				}

			} else {
				if(iconStyle == 'icon_none') {
					iconStyle = 'icon_left';
				}
			}
			if(iconStyle == 'icon_top') {
				$(newEdit).find('.icon_left_label').css("display", "none");
			} else
				$(newEdit).find('.icon_top_label').css("display", "none");
			g_data[key].prop["iconStyle"] = iconStyle;
			//初始化导航样式
			$(newPanel).find(".STH_navs_panel ul").removeClass("style1 style2 icon_top icon_left icon_none").addClass(navStyle + " " + iconStyle);
			$(newEdit).find(".nav_style_wrap input[type=radio]").prop('unchecked');
			$(newEdit).find(".nav_style_wrap input[data-style=" + navStyle + "]").iCheck('check');
			$(newEdit).find(".icon_style_wrap input[type=radio]").prop('unchecked');
			$(newEdit).find(".icon_style_wrap input[data-style=" + iconStyle + "]").iCheck('check');
			var newPanelLi = $(newPanel).find("#nav_tpl_panel").html(),
				newEditLi = $(newEdit).find("#nav_tpl_edit").html();

			//填充之前清空后台和预览容器中的模板内容
			$(newPanel).find(".STH_navs_panel ul").html('');
			$(newEdit).find(".com-list-box>ul").html('');

			//导航预览区
			for(var i = 0; i < listLen; i++) {
				var row = navLlist[i]; 
				
				 
				var newobj = {
						"FnavImg": navLlist[i]["FnavImg"],
						"FnavImgId": navLlist[i]["FnavImgId"],
						"FnavText": navLlist[i]["FnavText"],
						"FtextColor": navLlist[i]["FtextColor"],
						"FbgColor": navLlist[i]["FbgColor"],
						"Ftypeid": navLlist[i]["Ftypeid"],
						"Ftype": navLlist[i]["Ftype"]
					},
					nPLi = main.formatTemplate(newobj, newPanelLi),
					nELi = main.formatTemplate(newobj, newEditLi);

				$(newPanel).find(".STH_navs_panel ul").append(nPLi);
				$(newEdit).find(".com-list-box>ul").append(nELi);
				
                $(newEdit).find(".com-list-box .select_wrap.level1 select").eq(i).val(row.Ftype);
                $(newEdit).find(".com-list-box .weipage_url").eq(i).val(row.Ftypename);
           		
			}

			//延时顺序重定位
			var edLength = $(".hb_box .panel-comp").length;
			if($(".hb_box .panel-comp").length < 1 || order > parseInt($(".hb_box .panel-comp").eq(edLength - 1).attr("order"))) {
				$(".hb_box").append(newPanel);
			} else if(order < parseInt($(".hb_box .panel-comp").eq(0).attr("order"))) {
				$(".hb_box").prepend(newPanel);
			} else {
				for(var r = 0; r < edLength; r++) {
					if(order > parseInt($(".hb_box .panel-comp").eq(r).attr("order")) && order < parseInt($(".hb_box .panel-comp").eq(r + 1).attr("order"))) {
						$(".hb_box .panel-comp").eq(r).after(newPanel);
						break;
					}
				}
			}
			$(".edit-panel").append(newEdit);

			_this.navsActive(newPanel, newEdit, key);
		},

		//navs类型的常规操作
		navsActive: function(thisPanel, thisEdit, key) {
			var _this = this,
				upload_index = 0,
				imgloadinit, imgId = "imgupload" + key;

			if(typeof(g_data[key].ItemList) == "undefined"
			  || g_data[key].ItemList.length==0 ) {
				g_data[key].prop["navStyle"] = "style1";
				g_data[key].prop["iconStyle"] = "icon_top";
				g_data[key].ItemList = [{
						"FnavImg": "/../../css/skin/Content/Shop/images/chh_navs_icon.png",
						"FnavImgId": "default",
						"FnavText": "",
						"FtextColor": "#333",
						"FbgColor": "#fff",
						"Ftypeid": "",
						"Ftype": ""
					},
					{

						"FnavImg": "../../css/skin/Content/Shop/images/chh_navs_icon.png",
						"FnavImgId": "default",
						"FnavText": "",
						"FtextColor": "#333",
						"FbgColor": "#fff",
						"Ftypeid": "",
						"Ftype": ""
					},
					{
						"FnavImg": "/../../css/skin/Content/Shop/images/chh_navs_icon.png",
						"FnavImgId": "default",
						"FnavText": "",
						"FtextColor": "#333",
						"FbgColor": "#fff",
						"Ftypeid": "",
						"Ftype": ""
					},
					{
						"FnavImg": "/../../css/skin/Content/Shop/images/chh_navs_icon.png",
						"FnavImgId": "default",
						"FnavText": "",
						"FtextColor": "#333",
						"FbgColor": "#fff",
						"Ftypeid": "",
						"Ftype": ""
					},
					{
						"FnavImg": "/../../css/skin/Content/Shop/images/chh_navs_icon.png",
						"FnavImgId": "default",
						"FnavText": "",
						"FtextColor": "#333",
						"FbgColor": "#fff",
						"Ftypeid": "",
						"Ftype": ""
					}
				];
				$(thisEdit).find('.icon_left_label').css("display", "none");

			}

			/*icheck初始化*/
			$(thisEdit).find('input').iCheck(main.iCheckOption);

			/*选择导航类型 */
			//导航：style1,分组：style2
			$(thisEdit).find(".nav_style_wrap input").on('ifChecked', function() {
				var navStyle = $(this).attr("data-style");
				if(navStyle == 'style1' || navStyle == 'style2' || navStyle == 'style3' || navStyle == 'style4') {
					$(thisEdit).find('.icon_left_label').css("display", "none");
					$(thisEdit).find('.icon_top_label').css("display", "block");
					if(g_data[key].prop["iconStyle"] == "icon_left") {
						$(thisEdit).find('.icon_top_label').find('input').iCheck('check');
					}
				} else {
					$(thisEdit).find('.icon_top_label').css("display", "none");
					$(thisEdit).find('.icon_left_label').css("display", "block");
					if(g_data[key].prop["iconStyle"] == "icon_top") {
						$(thisEdit).find('.icon_left_label').find('input').iCheck('check');
					}
				}
				$(thisPanel).find(".STH_navs_panel ul").removeClass("style1 style2 style3 style4 style5").addClass(navStyle);
				g_data[key].prop["navStyle"] = navStyle;
			});

			/*选择图标类型*/
			//图标居上：icon_top,图标居左：icon_left,无图标：icon_none
			$(thisEdit).find(".icon_style_wrap input").on('ifChecked', function() {
				var iconStyle = $(this).attr("data-style");
				$(thisPanel).find(".STH_navs_panel ul").removeClass("icon_top icon_left icon_none").addClass(iconStyle);
				g_data[key].prop["iconStyle"] = iconStyle;
			});

			/*添加导航项*/
			$(thisEdit).find(".btn_add_box .btn").on('click', function() {
				var dataObject = {
						"FnavImg": "../../css/skin/Content/Shop/images/chh_navs_icon.png",
						"FnavImgId": "default",
						"FnavText": "导航",
						"FtextColor": "#333",
						"FbgColor": "#fff",
						"Ftypeid": "",
						"Ftype": ""
					},
					tempmb = $(thisEdit).find("#nav_tpl_edit").html(),
					tempmb2 = $(thisPanel).find("#nav_tpl_panel").html(),
					str = main.formatTemplate(dataObject, tempmb),
					str2 = main.formatTemplate(dataObject, tempmb2);

				$(thisEdit).find(".com-list-box>ul").append(str);
				$(thisPanel).find(".STH_navs_panel .navlist").append(str2);

				var index = $(thisEdit).find(".com-list-box>ul>li").last().index();

				$(thisEdit).find(".com-list-box .select_wrap.level1 select").last()
					.append("请选择链接<option value='微页面'>微页面</option><option value='外部链接'>外部链接</option> ");

				g_data[key].ItemList[index] = {
					"FnavImg": "/../../css/skin/Content/Shop/images/chh_navs_icon.png",
					"FnavImgId": "default",
					"FnavText": "",
					"FtextColor": "#333",
					"FbgColor": "#fff",
					"Ftypeid": "",
					"Ftype": ""
				}

				navimgUpload(thisPanel, thisEdit, key);
				navColorset(thisPanel, thisEdit, key);
				initText(thisPanel, thisEdit, key);
				selectOne();
				selectTwo();

			});

			function initText(thisPanel, thisEdit, key) {
				var obj = $(thisEdit).find(".weipage_url");
				$(obj).unbind("click").click(function() {
					var index = $(thisEdit).find(".weipage_url").index(this);

					var selText = $(this).parents(".navs_set").find("select.sel_1").val();
					var weipage_url = $(this);
					var row =g_data[key].ItemList[index];
					row.Ftype = selText;
					if(selText == "1") { //微页面
						main.commonDialog('vstore/tb_page/Sql.html', '选择微页面', 800, 600, function(res) {

							$(weipage_url).val(res.PageName);
							g_data[key].ItemList[index].Ftype = 1;
							g_data[key].ItemList[index].Ftypeid = res.ID;

						});

					} else if(selText == "2") { //商品分类
						main.typeDialog(function(res) {

							$(weipage_url).val(res.Fname);
							g_data[key].ItemList[index].Ftype = 2;
							g_data[key].ItemList[index].Ftypeid = res.Fid;
 
						});

					}
				});

			}
			/*导航图片上传*/
			$(thisEdit).find(".imgupload").attr("id", imgId);

			function navimgUpload(thisPanel, thisEdit, key) {
				$(thisEdit).find(".com-list-box .com-list-box-choice-pic").unbind("click").click(function() {
					upload_index = $(thisEdit).find(".com-list-box .com-list-box-choice-pic").index(this);
					main.photoDialog(function(res) {
						var path = res[0];
						$(thisEdit).find(".com-list-box .com-list-box-choice").eq(upload_index).find(".com-list-box-choice-pic img").attr("src", path);
						$(thisPanel).find(".STH_navs_panel ul .icon").eq(upload_index).find("img").attr("src", path);

						if(g_data[key].ItemList[upload_index]) {
							g_data[key].ItemList[upload_index].FnavImg = path;
						} else {
							g_data[key].ItemList[upload_index] = {
								"FnavImg": path
							};
						}

					});
				});

			}
			navimgUpload(thisPanel, thisEdit, key);

			/*导航文字设置*/
			$(thisEdit).find(".com-list-box").on('blur', 'input.nav_text', function() {
				var $this = $(this),
					index = $(thisEdit).find(".com-list-box .com-list-box-choice").index($this.parents(".com-list-box-choice")),
					val = filterXSS(main.html_encode($(this).val()));
				console.log(index);

				if($this.val() == "") {

					$this.attr('value', val);
					$(thisPanel).find(".STH_navs_panel .navlist .text").eq(index).html(val);
					g_data[key].ItemList[index].FnavText = val;

					layer.msg('导航文字不能为空', {
						"icon": 0
					});

				} else if($this.val().length > 6) {

					val = val.slice(0, 6);

					$this.attr('value', val);
					$(thisPanel).find(".STH_navs_panel .navlist .text").eq(index).html(val);
					g_data[key].ItemList[index].FnavText = val;

					layer.msg('导航文字最多6个字', {
						"icon": 0
					});

				} else {
					$this.attr('value', val);
					$(thisPanel).find(".STH_navs_panel .navlist .text").eq(index).html(val);
					g_data[key].ItemList[index].FnavText = val;

				}
			});

			//导航颜色设置
			function navColorset(thisPanel, thisEdit, key) {
				$(thisEdit).find('.com-list-box .navs_set .textcolor').ColorPicker({
					color: g_data[key].prop["FtextColor"],
					onShow: function(colpkr) {
						$(colpkr).fadeIn(500);
						return false;
					},
					onHide: function(colpkr) {
						$(colpkr).fadeOut(500);
						return false;
					},
					onSubmit: function(hsb, hex, rgb, el) {
						$(el).ColorPickerHide();
						return false;
					},
					onChange: function(hsb, hex, rgb, el) {
						var index = $(thisEdit).find(".com-list-box .com-list-box-choice").index($(el).parents(".com-list-box-choice"));

						$(el).css('backgroundColor', '#' + hex);
						$(thisPanel).find(".STH_navs_panel .navlist .text").eq(index).css('color', '#' + hex);
						g_data[key].ItemList[index]["FtextColor"] = '#' + hex;
					}
				});

				$(thisEdit).find('.com-list-box .navs_set .bgcolor').ColorPicker({
					color: g_data[key].prop["FbgColor"],
					onShow: function(colpkr) {
						$(colpkr).fadeIn(500);
						return false;
					},
					onHide: function(colpkr) {
						$(colpkr).fadeOut(500);
						return false;
					},
					onSubmit: function(hsb, hex, rgb, el) {
						$(el).ColorPickerHide();
						return false;
					},
					onChange: function(hsb, hex, rgb, el) {
						var index = $(thisEdit).find(".com-list-box .com-list-box-choice").index($(el).parents(".com-list-box-choice"));

						$(el).css('backgroundColor', '#' + hex);
						$(thisPanel).find(".STH_navs_panel .navlist li").eq(index).css('background-color', '#' + hex);
						g_data[key].ItemList[index]["FbgColor"] = '#' + hex;
					}
				});
			}
			navColorset(thisPanel, thisEdit, key);

			//一级链接选择
			function selectOne() {
				$(thisEdit).find(".select_wrap.level1 select").selectModify({
					onSelectChange: function(e, attrs) {
						var one_val = $(e).val();
						var onindex = $(e).parents(".com-list-box .com-list-box-choice").index();

						g_data[key].ItemList[onindex].Ftype = one_val;

						selectTwo(onindex);
					}
				});
			}
			selectOne();

			//二级链接选择函数
			function selectTwo(t) {
				$(thisEdit).find('.navs_set .weipage_url').eq(t).val("");
				var item = g_data[key].ItemList[t];
				if(item) {
					item.Ftypeid = 0;
				}
			}
			selectTwo();

			/*导航排序*/
			function navSort(thisPanel, thisEdit, key) {

				//上移一个位置				
				$(thisEdit).undelegate(".com-list-box-operate .arrow-up", 'click').delegate(".com-list-box-operate .arrow-up", 'click', function() {
					var index = $(thisEdit).find(".com-list-box .com-list-box-operate .arrow-up").index(this);

					if(index > 0) {
						var htmlE = $(this).parents("li").prop("outerHTML"),
							htmlP = $(thisPanel).find(".STH_navs_panel ul>a:eq(" + index + ")").prop("outerHTML"),
							sel1val = $(thisEdit).find(".com-list-box>ul>li:eq(" + (index) + ")").find(".level1 select").val(),
							sel1va2 = $(thisEdit).find(".com-list-box>ul>li:eq(" + (index) + ")").find(".level2 select").val();

						$(thisEdit).find(".com-list-box>ul>li:eq(" + (index - 1) + ")").before(htmlE);
						$(thisEdit).find(".com-list-box>ul>li:eq(" + (index + 1) + ")").remove();
						$(thisPanel).find(".STH_navs_panel ul>a:eq(" + (index - 1) + ")").before(htmlP);
						$(thisPanel).find(".STH_navs_panel ul>a:eq(" + (index + 1) + ")").remove();
						$(thisEdit).find(".com-list-box>ul>li:eq(" + (index - 1) + ")").find(".level1 select").val(sel1val);
						$(thisEdit).find(".com-list-box>ul>li:eq(" + (index - 1) + ")").find(".level2 select").val(sel1va2);
						main.exchange(g_data[key].ItemList, (index - 1), index);
						navimgUpload(thisPanel, thisEdit, key);
						navColorset(thisPanel, thisEdit, key);
						selectOne();
						selectTwo();
					}

				});

				//下移一个位置
				$(thisEdit).undelegate(".com-list-box-operate .arrow-down", 'click').delegate(".com-list-box-operate .arrow-down", 'click', function() {
					var index = $(thisEdit).find(".com-list-box .com-list-box-operate .arrow-down").index(this),
						lilen = $(thisEdit).find(".com-list-box .com-list-box-operate .arrow-down").length;

					if(index < (lilen - 1)) {
						var htmlE = $(this).parents("li").prop("outerHTML"),
							htmlP = $(thisPanel).find(".STH_navs_panel ul>a:eq(" + index + ")").prop("outerHTML"),
							sel1val = $(thisEdit).find(".com-list-box>ul>li:eq(" + (index) + ")").find(".level1 select").val(),
							sel1va2 = $(thisEdit).find(".com-list-box>ul>li:eq(" + (index) + ")").find(".level2 select").val();
						$(thisEdit).find(".com-list-box>ul>li:eq(" + (index + 1) + ")").after(htmlE);
						$(thisEdit).find(".com-list-box>ul>li:eq(" + index + ")").remove();
						$(thisPanel).find(".STH_navs_panel ul>a:eq(" + (index + 1) + ")").after(htmlP);
						$(thisPanel).find(".STH_navs_panel ul>a:eq(" + index + ")").remove();
						$(thisEdit).find(".com-list-box>ul>li:eq(" + (index + 1) + ")").find(".level1 select").val(sel1val);
						$(thisEdit).find(".com-list-box>ul>li:eq(" + (index + 1) + ")").find(".level2 select").val(sel1va2);
						main.exchange(g_data[key].ItemList, index, (index + 1));
						navimgUpload(thisPanel, thisEdit, key);
						navColorset(thisPanel, thisEdit, key);
						selectOne();
						selectTwo();
					}
				});

			}
			navSort(thisPanel, thisEdit, key);

			/*导航删除操作*/
			function navDel(thisPanel, thisEdit, key) {
				$(thisEdit).undelegate(".com-list-box-delete i", 'click').delegate(".com-list-box-delete i", 'click', function() {

					var shunxu = $(thisEdit).find(".com-list-box .com-list-box-delete i").index(this),
						htmlP = $(main.getPanel("navs")).find(".STH_navs_panel a:eq(0)").prop("outerHTML"),
						htmlE = $(main.getEdit("navs")).find(".STH_navs_edit .com-list-box ul li:eq(0)").prop("outerHTML");
					//console.log(htmlE);
					if($(thisPanel).find(".STH_navs_panel ul>a").length <= 1) {

						$(thisPanel).find(".STH_navs_panel ul").html(htmlP);
					} else {

						$(thisPanel).find(".STH_navs_panel ul>a:eq('" + shunxu + "')").remove();

					}

					if($(thisEdit).find(".com-list-box>ul>li").length <= 1) {

						$(thisEdit).find(".com-list-box>ul").html(htmlE);

						layer.msg('只有一个导航了，内容已清空', {
							"icon": 0
						});

						g_data[key].ItemList[0] = {
							"FnavImg": "/../../css/skin/Content/Shop/images/chh_navs_icon.png",
							"FnavImgId": "default",
							"FnavText": "",
							"FtextColor": "#333",
							"FbgColor": "#fff",
							"Ftypeid": "",
							"Ftype": "" //微页面 分类 
						}

						//商户版本
//						if(sellerVersion != 2 && sellerVersion != 3) {
//							$(thisEdit).find(".com-list-box .select_wrap.level1 select").eq(0).append("<option value='微页面'>微页面</option><option value='商品分类'>商品分类</option><option value='服务分类'>服务分类</option><option value='服务人员等级'>服务人员等级</option>");
//						} else {
							$(thisEdit).find(".com-list-box .select_wrap.level1 select").eq(0).append("<option value='微页面'>微页面</option><option value='商品分类'>商品分类</option>");
//						}

						navimgUpload(thisPanel, thisEdit, key);
						navColorset(thisPanel, thisEdit, key);
						navDel(thisPanel, thisEdit, key);
						selectOne();
						selectTwo();

					} else {

						$(this).parents("li").remove();

						g_data[key].ItemList.splice(shunxu, 1);

					}

				});
			}
			navDel(thisPanel, thisEdit, key);

			initText(thisPanel, thisEdit, key);
			//调用主模板JS
			main.saveClick();
			main.moduleActive();
			main.dragEdit();
		}

		/**--navs类型end--*/

	}
});